<?php

namespace Apps\Common\Service;

use Apps\Common\Constant\TaskConstant;
use Apps\Common\Model\TaskModel;
use Phalcon\Di;
use Vendor\Pheanstalk\PheanstalkInterface;

/**
 * 异步任务服务类
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2015/7/16 10:39
 * @copyright  deyi.com
 */
class TaskService
{
    /**
     *  执行异步任务
     *
     * @author liang<lang@vip.deyi.com>
     * @param $taskName
     * @param $ext
     */
    public static function doAsyncTask($taskName, $ext)
    {
        /**
         * @var $beanstalkd \Vendor\Pheanstalk\Pheanstalk
         */
        $beanstalkd = Di::getDefault()->get('beanstalkd');

        $taskModel = new TaskModel();
        $taskModel->setName($taskName);
        $taskModel->setExtra($ext);
        $taskModel->setTime(time());

        $beanstalkd->useTube(Di::getDefault()->get('config')->project->name . TaskConstant::BACKGROUNDTASK)->put(
            serialize($taskModel),
            PheanstalkInterface::DEFAULT_PRIORITY,
            PheanstalkInterface::DEFAULT_DELAY,
            900
        );
    }

}
